package com.westa.smallshop.common;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;

import com.westa.smallshop.data.Base;
import com.westa.smallshop.data.User;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by Dune on 16/8/8.
 */
public abstract class BaseListAdapter<VH extends RecyclerView.ViewHolder, O extends Base> extends RecyclerViewBaseAdapter<VH> {

    public List<O> mData = new ArrayList<>();

    public BaseListAdapter() {
    }

    public BaseListAdapter(List<O> data) {
        mData = checkNotNull(data);
    }

    public void refreshData(List<O> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void loadMoreData(List<O> data) {
        mData.addAll(checkNotNull(data));
        notifyDataSetChanged();
    }

    public void cleanData() {
        mData.clear();
        notifyDataSetChanged();
    }

    public void updateItemObject(O base, int position) {

        mData.set(position, base);
        notifyItemChanged(position);
    }

    public void removeItem(int position) {

        mData.remove(position);
        notifyItemRemoved(position);
    }

    public O getItemObject(int position) {
        return mData.get(position);
    }


    public Long getLastCreateTime() {
        if (mData.size() > 0) {

            return mData.get(mData.size() - 1).getCreated_time();
        }

        return null;
    }

    public Long getLastUpdateTime() {

        if (mData.size() > 0) {
            return mData.get(mData.size() - 1).getUpdate_time();
        }
        return null;
    }

    @Nullable
    public User getCreater(int position) {

        return mData.get(position).getCreater();
    }


    @Override
    public long getItemId(int position) {
        return mData.get(position).getId();
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}
